package com.cfh.springcloudclientapplication.event;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.EventListener;

public class SpringAnnotationDrivenEvent {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        context.register(SpringAnnotationDrivenEvent.class);

        context.refresh();

        context.publishEvent(new MyApplicationEvent("Hello,World"));

        context.close();
    }


    private static class MyApplicationEvent extends ApplicationEvent {

        public MyApplicationEvent(Object source) {
            super(source);
        }
    }

    @EventListener
    public void onMessage(MyApplicationEvent event) {
        System.err.println("监听到事件源1：" + event.getSource());
    }

    @EventListener
    public void onMessage(Object event) {
        System.err.println("监听到事件源2：" + event);
    }
}
